package com.tqjc.system.core.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.tqjc.system.core.config.properties.DataSourceProperties;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@Configuration
//@EnableTransactionManagement
//@MapperScan(basePackages = {"com.tqjc.**.dao"},sqlSessionFactoryRef = "systemSqlSessionFactory")
public class DbConfig {

//    private final MybatisplusDSP properties;
//
//    private final List<ConfigurationCustomizer> configurationCustomizers;
//
//    private final ApplicationContext applicationContext;
//
//    public DbConfig(MybatisplusDSP properties,
//                    ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
//                    ApplicationContext applicationContext) {
//        this.properties = properties;
//        this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
//        this.applicationContext = applicationContext;
//    }
//
//    @Autowired
//    @Qualifier("system_DSP")
//    private DataSourceProperties dataSourceProperties;
//
//    /**
//     * 初始化properties
//     * @return
//     */
//    @Bean(name = "system_DSP")
//    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.system")
//    public DataSourceProperties dataSourceProperties() {
//        return new DataSourceProperties();
//    }
//
//    /**
//     * druid数据库连接池
//     */
//    @Bean(initMethod = "init", destroyMethod="close",name = "system_DS")
//    public DruidDataSource dataSource() {
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSourceProperties.initTo(dataSource);
//        return dataSource;
//    }
//
//    @Primary
//    @Bean(name = "systemTransactionManager")
//    public DataSourceTransactionManager transactionManager(@Qualifier("system_DS") DataSource dataSource) throws SQLException {
//        return new DataSourceTransactionManager(dataSource);
//    }
//
//    @Primary
//    @Bean(name = "systemSqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("system_DS") DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
//        factory.setDataSource(dataSource);
//        factory.setVfs(SpringBootVFS.class);
//        applyConfiguration(factory);
//        factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/**/xml/*.xml"));
//        // TODO 此处必为非 NULL
//        GlobalConfig globalConfig = this.properties.getGlobalConfig();
//        //注入填充器
//        if (this.applicationContext.getBeanNamesForType(MetaObjectHandler.class,
//                false, false).length > 0) {
//            MetaObjectHandler metaObjectHandler = this.applicationContext.getBean(MetaObjectHandler.class);
//            globalConfig.setMetaObjectHandler(metaObjectHandler);
//        }
//        factory.setGlobalConfig(globalConfig);
//        return factory.getObject();
//    }
//
//    private void applyConfiguration(MybatisSqlSessionFactoryBean factory) {
//        MybatisConfiguration configuration = this.properties.getConfiguration();
//        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
//            configuration = new MybatisConfiguration();
//        }
//        if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
//            for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
//                customizer.customize(configuration);
//            }
//        }
//        factory.setConfiguration(configuration);
//    }
//
//    @Primary
//    @Bean(name = "sytemSqlSessionTemplate")
//    @ConditionalOnMissingBean
//    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("systemSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        ExecutorType executorType = this.properties.getExecutorType();
//        if (executorType != null) {
//            return new SqlSessionTemplate(sqlSessionFactory, executorType);
//        } else {
//            return new SqlSessionTemplate(sqlSessionFactory);
//        }
//    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DbType.MYSQL.getDb());
        return paginationInterceptor;
    }

    /**
     * 乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}
